package com.example.gw_back_end.controller;

import com.example.gw_back_end.domain.Result;
import com.example.gw_back_end.service.mongodb.CartService;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("cart")
public class CartController {
    private final CartService cartService;
    public CartController(CartService cartService) {
        this.cartService = cartService;
    }

    @PostMapping
    public Result addGameGood(Integer gameId) {
        if (cartService.saveShoppingCart(gameId)  != null ){
            return Result.success();
        } else {
            return Result.error();
        }
    }

    @DeleteMapping
    public Result removeGood(Integer gameId) {
        System.out.println(gameId);
        cartService.deleteShoppingCart(gameId.toString());
        return Result.success();
    }

    @PutMapping
    public Result updateGood(Integer gameId) {
        return Result.success();
    }

    @GetMapping
    public Result getCart() {
        return Result.success(cartService.getShoppingCart().getItems());
    }

    @DeleteMapping("clear")
    public Result clearCart(){
        cartService.clearShoppingCart();
        return Result.success();
    }
}
